<?php
namespace Template;
// 字符编码
use app\applet\model\HouseKeeper;
use app\manage\model\AdminUser;
use think\Db;

header("Content-Type:text/html; charset=utf-8");

// 微信接口类 $i=new \Template\Wechat();
class WeChat{
    private static $appid;
    private static $appsecret;

    function __construct(){
        self::$appid = config('subscription')['appid'];     // 开发者ID(AppID)
        self::$appsecret =config('subscription')['secret'];  // 开发者密码(AppSecret)
    }

    // 微信授权地址
    public static function getAuthorizeUrl($url){
        $url_link = urlencode($url);
        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . self::$appid . "&redirect_uri={$url_link}&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
    }

    // 获取TOKEN
    public static function getToken(){
        //将AccessToken存入session
        $res = Db::name('wechat')->where(['wechat_id'=>1])->find();
        if($res['access_token'] && $res['expire_time'] > time())
        {
            //如果access_token在session中并没有过期
            return $res['access_token'];
        }else{
            //如果不存在或者已经过期,重新去取access_token
            $urla = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::$appid . "&secret=" . self::$appsecret;
            $outputa = self::curlGet($urla);
            $result = json_decode($outputa, true);
            //将重新取得access_token存入session
            $expire_time = time()+7000;
            Db::name('wechat')->where(['wechat_id'=>1])->update(['access_token'=>$result['access_token'],'expire_time'=>$expire_time]);
            return $result['access_token'];
        }
    }

//    /**
//     * getUserInfo 获取用户信息
//     * @param  string $code         微信授权code
//     * @param  string $weiwei_token Token
//     * @return array
//     */
//    public static function getUserInfo($code, $weiwei_token){
//        $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$appid . "&secret=" . self::$appsecret . "&code={$code}&grant_type=authorization_code";
//        $access_token_json = self::curlGet($access_token_url);
//        $access_token_array = json_decode($access_token_json, true);
//        $openid = $access_token_array['openid'];
//        $new_access_token = $weiwei_token;
//
//        //全局access token获得用户基本信息
//        $userinfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$new_access_token}&openid={$openid}";
//        $userinfo_json = self::curlGet($userinfo_url);
//        $userinfo_array = json_decode($userinfo_json, true);
//        return $userinfo_array;
//    }
    public static function getUserInfo($tel, $code='') {
        $model = new HouseKeeper();
        $detail = $model->uniqueness($tel);
        if (empty($detail['wechat_subscription_open_id'])) {
            $weiwei_token = self::getToken();
            $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$appid . "&secret=" . self::$appsecret . "&code={$code}&grant_type=authorization_code";
            $access_token_json = self::curlGet($access_token_url);
            $access_token_array = json_decode($access_token_json, true);
            if (!isset($access_token_array['openid'])) return false;
            $openid = $access_token_array['openid'];
            $new_access_token = $weiwei_token;

            //全局access token获得用户基本信息
            $userinfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$new_access_token}&openid={$openid}";
            $userinfo_json = self::curlGet($userinfo_url);
            $userinfo_array = json_decode($userinfo_json, true);

            $model->m_update($detail['id'], ['wechat_subscription_open_id' => $userinfo_array['openid']]);
            return $userinfo_array['openid'];
        } else {
            return $detail['wechat_subscription_open_id'];
        }
    }

    /**
     * @param $tel
     * @param string $code
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function getAdminInfo($tel, $code='') {
        $model = new AdminUser();
        $detail = $model->detailTel($tel);
        if (empty($detail['wechat_open_id'])) {
            $weiwei_token = self::getToken();
            $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$appid . "&secret=" . self::$appsecret . "&code={$code}&grant_type=authorization_code";
            $access_token_json = self::curlGet($access_token_url);
            $access_token_array = json_decode($access_token_json, true);
            $openid = $access_token_array['openid'];
            $new_access_token = $weiwei_token;

            //全局access token获得用户基本信息
            $userinfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$new_access_token}&openid={$openid}";
            $userinfo_json = self::curlGet($userinfo_url);
            $userinfo_array = json_decode($userinfo_json, true);

            $model->m_update($detail['admin_id'], ['wechat_open_id' => $userinfo_array['openid']]);
            return $userinfo_array['openid'];
        } else {
            return $detail['wechat_open_id'];
        }
    }

    /**
     * pushMessage 发送自定义的模板消息
     * @param  array  $data          模板数据
    $data = [
        'openid' => '', 用户openid
        'url' => '', 跳转链接
        'template_id' => '', 模板id
        'data' => [ // 消息模板数据
            'first'    => ['value' => urlencode('黄旭辉'),'color' => "#743A3A"],
            'keyword1' => ['value' => urlencode('男'),'color'=>'blue'],
            'keyword2' => ['value' => urlencode('1993-10-23'),'color' => 'blue'],
            'remark'   => ['value' => urlencode('我的模板'),'color' => '#743A3A']
        ]
    ];
     * @param  string $topcolor 模板内容字体颜色，不填默认为黑色
     * @return array
     */
    public static function pushMessage($data = [],$topcolor = '#0000'){
        $template = [
            'touser'      => $data['openid'],
            'template_id' => $data['template_id'],
            //'url'         => 'index',
            'topcolor'    => $topcolor,
            'data'        => $data['data'],
            'miniprogram'        => $data['miniprogram']
        ];
        $json_template = json_encode($template);
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . self::getToken();
        $result = self::curlPost($url, urldecode($json_template));

        $resultData = json_decode($result, true);
        return $resultData;
    }

    /**
     * addLog 日志记录
     * @param string $log_content 日志内容
     */
    public static function addLog($log_content = ''){
        $data = "";
        $data .= "DATE: [ " . date('Y-m-d H:i:s') . " ]\r\n";
        $data .= "INFO: " . $log_content . "\r\n\r\n";
        file_put_contents('/wechat.log', $data, FILE_APPEND);
    }

    /**
     * 发送get请求
     * @param string $url 链接
     * @return bool|mixed
     */
    private static function curlGet($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        if(curl_errno($curl)){
            return 'ERROR ' . curl_error($curl);
        }
        curl_close($curl);
        return $output;
    }

    /**
     * 发送post请求
     * @param string $url 链接
     * @param string $data 数据
     * @return bool|mixed
     */
    private static function curlPost($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if(!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}



